//
//  EFengContent.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/2/29.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengContent.h"
#import "MJExtension.h"
#import "NSString+CBExtension.h"
#import "WZAttributedAction.h"

@implementation EFengContent

+ (NSDictionary *)replacedKeyFromPropertyName
{
    return @{@"shareId":@"contentId",@"commentId":@"id",@"commentType":@"type"};
}

- (NSAttributedString *)contentAttr
{
    __block EFengContent *weakSelf = self;
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] init];
    NSAttributedString *contentAttr = [self.content cookEmotionStringWithFont:Font14 textColor:RGBCOLORWITH16COLOR(0x646464)];

    NSAttributedString *sendNameAttr = [[NSAttributedString alloc] initWithString:self.sendUserName attributes:@{NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x1e628f),NSFontAttributeName:Font14,@"action":[[WZAttributedAction alloc] initWithAction:^() {
        [[NSNotificationCenter defaultCenter] postNotificationName:WZAttributedActionDidClick object:weakSelf.sendUserId];
        
    }]}];
    
    NSAttributedString *moahao = [[NSAttributedString alloc] initWithString:@"：" attributes:@{NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x1e628f),NSFontAttributeName:Font14}];
    [attr appendAttributedString:sendNameAttr];
    
    
    if (!M_ISSTRNULL(self.receiveUserId)) {
        NSAttributedString *huifu = [[NSAttributedString alloc] initWithString:@"回复" attributes:@{NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x646464),NSFontAttributeName:Font14}];
        NSAttributedString *receiveUserNameAttr = [[NSAttributedString alloc] initWithString:self.receiveUserName attributes:@{NSForegroundColorAttributeName:RGBCOLORWITH16COLOR(0x1e628f),NSFontAttributeName:Font14,@"action":[[WZAttributedAction alloc] initWithAction:^() {
            [[NSNotificationCenter defaultCenter] postNotificationName:WZAttributedActionDidClick object:weakSelf.receiveUserId];
            
        }]}];
        
        [attr appendAttributedString:huifu];
        [attr appendAttributedString:receiveUserNameAttr];
        
    }
    [attr appendAttributedString:moahao];
    [attr appendAttributedString:contentAttr];
    return [self attributedTextWithLinksWithStr:attr];
}

//添加url
-(NSMutableAttributedString*)attributedTextWithLinksWithStr:(NSMutableAttributedString *)attrString
{
    NSMutableAttributedString* str = [attrString mutableCopy];
    if (!str) return nil;
    
    NSString* plainText = [str string];
    if (plainText )
    {
        NSError* error = nil;
        
        NSRegularExpression *urlRegex = [NSRegularExpression regularExpressionWithPattern: @"((http[s]{0,1}|ftp|items)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((www.)?([a-zA-Z0-9\\-]+\\.)+([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(((http[s]{0,1}|ftp)://)?(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\\.((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){2}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)" options:NSRegularExpressionCaseInsensitive error:&error];
        
        
        [urlRegex enumerateMatchesInString:plainText options:0 range:NSMakeRange(0, [plainText length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
            
            NSString *acStr = [plainText substringWithRange:result.range];
            
            [str removeAttribute:NSForegroundColorAttributeName range:[result range]]; // Work around for Apple leak
            [str addAttribute:NSForegroundColorAttributeName value:RGBCOLORWITH16COLOR(0x1e628f) range:[result range]];
            [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:result.range];
            [str addAttribute:@"action" value:[[WZAttributedAction alloc] initWithAction:^{
                NSString *phoneRegex = @"^((https://)|(http://)|(items://)(ftp://))";
                NSRegularExpression *phoneTest = [NSRegularExpression regularExpressionWithPattern:phoneRegex options:NSRegularExpressionCaseInsensitive error:nil];
                NSTextCheckingResult *range = [phoneTest firstMatchInString:acStr options:0 range:NSMakeRange(0, acStr.length)];
                if (range) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[M_STOREVALUE(acStr) stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
                } else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[[NSString stringWithFormat:@"http://%@",M_STOREVALUE(acStr)] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
                }
                
                
            }] range:[result range]];
        }];
        
        //        NSRegularExpression *phoneNumber = [NSRegularExpression regularExpressionWithPattern: @"\\d{3}-\\d{8}|\\d{3}-\\d{7}|\\d{4}-\\d{8}|\\d{4}-\\d{7}|1+[358]+\\d{9}|\\d{8}|\\d{7}" options:0 error:&error];
        //        [phoneNumber enumerateMatchesInString:plainText options:0 range:NSMakeRange(0, [plainText length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        //
        //            NSString *acStr = [plainText substringWithRange:result.range];
        //
        //            [str removeAttribute:NSForegroundColorAttributeName range:[result range]]; // Work around for Apple leak
        //            [str addAttribute:NSForegroundColorAttributeName value:RGBCOLORWITH16COLOR(0x1e628f) range:[result range]];
        //            [str addAttribute:@"action" value:[[WZAttributedAction alloc] initWithAction:^{
        //                UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"%@可能是一个电话号码,你可以",acStr] delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"呼叫" otherButtonTitles:@"复制",@"保存到通讯录", nil];
        //                [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
        //
        //
        //            }] range:[result range]];
        //            
        //        }];
        
    }
    return str;
}


@end
